package com.flink.api.unit4;

import com.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * map_1
 *
 * @author caizhiyang
 * @since 2024-02-21
 */
public class map_1 {

    /**
     * map算子
     * @param args
     */
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<WaterSensor> source = env.fromElements(
                new WaterSensor("sensor_1", 1L, 1),
                new WaterSensor("sensor_2", 2L, 2)
        );

        //方式一: 使用MapFunction
       /* SingleOutputStreamOperator<String> singleOutputStreamOperator = source.map(new MapFunction<WaterSensor, String>() {
            @Override
            public String map(WaterSensor waterSensor) throws Exception {
                return waterSensor.getId();
            }
        });
        singleOutputStreamOperator.print();*/

        //方式二: lambda表达式
        SingleOutputStreamOperator<String> singleOutputStreamOperator1 = source.map((waterSensor) -> {
            return waterSensor.getId();
        });
        singleOutputStreamOperator1.print();

        env.execute();
    }
}
